﻿
namespace MetroNextTools.NBase
{
    /// <summary>
    /// 挂起类的抽象，组织组装某些功能直到Resume时，也就是一个阻断类
    /// (eg: Represents the abstract definition of an object which may be suspended. That is to prevent it from prefroming certain functionality until resumed.)
    /// </summary>
    public class SuspendableObject
    {
        private byte suspendCount;

        public bool IsSuspended
        {
            get
            {
                return this.suspendCount > 0;
            }
        }

        public void Suspend()
        {
            this.suspendCount += 1;
            //第一次挂起
            if (this.suspendCount == 1)
            {
                this.OnSuspend();
            }
        }

        public void Resume()
        {
            this.Resume(true);
        }

        private void Resume(bool update)
        {
            if (this.suspendCount == 0)
            {
                return;
            }
            this.suspendCount -= 1;
            if (this.suspendCount == 0)
            {
                this.OnResume(update);
            }
        }

        protected virtual void OnSuspend()
        {
            
        }

        protected virtual void OnResume(bool update)
        {
            
        }
    }
}
